package zero.tech.games.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import zero.tech.games.entity.user.TUserAccount;
import zero.tech.redis.cache.RedisUtil;
import zero.tech.redis.topic.TopicChannelType;
import zero.tech.redis.topic.service.api.IRedisMsgService;


@Component
public class TUserAccountUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private IRedisMsgService redisMsgService;
	public void insertOrUpdate(TUserAccount userAccount) {
		int uid=userAccount.getUid();
		redisUtil.hsetJson(RedisConsts.A_USERACCOUNT,String.valueOf(uid), userAccount);
	}
	public TUserAccount getUserAccount(Integer uid) {
		return redisUtil.hgetJson(RedisConsts.A_USERACCOUNT,String.valueOf(uid), TUserAccount.class);
	}
	public void notifyDiamondChange(int... uid) {
		List<Integer> uids=new ArrayList<>();
		for(int id:uid) {
			uids.add(id);
		}
		redisMsgService.pulishRedisTopic(TopicChannelType.DIAMOND_CHANGE, TopicOrder.DIAMOND_CHANGE, uids);
	}
	public void insertOrUpdateBatch(List<TUserAccount> userAccounts) {
		Map<Object,Object>map=new HashMap<>();
		for(TUserAccount userAccount:userAccounts) {
			map.put(String.valueOf(userAccount.getUid()), JSONObject.toJSONString(userAccount));
		}
		redisUtil.hmset(RedisConsts.A_USERACCOUNT, map);
	}
	
}
